<?php
class ModelOrderReturnStatus extends Model {
	public function addReturnStatus($data) {
		$name = $this->db->escape($data['name']);
		$sql = "INSERT INTO " . DB_PREFIX . "return_status SET `name` = '" . $name . "'";
		$this->db->query($sql);
		$return_status_id = $this->db->getLastId();
		
		foreach ($data['return_status_description'] as $language_id => $value) {
			$sql_desc = "INSERT INTO " . DB_PREFIX . "return_status_description SET return_status_id = '" . (int)$return_status_id . 
				"', language_id = '" . (int)$language_id . 
				"', display_name = '";
			if($value['display_name']){
				$sql_desc .= $this->db->escape($value['display_name']);
			}else{
				$sql_desc .= $name;
			}
			$sql_desc .= "', description = '" . $this->db->escape($value['description']) . "'";
			$this->db->query($sql_desc);
		}	
	}
	
	public function editReturnStatus($return_status_id, $data) {
		$name = $this->db->escape($data['name']);
		$sql = "UPDATE " . DB_PREFIX . "return_status SET `name` = '" . $name . "' WHERE return_status_id = '" . (int)$return_status_id . "'";
		$this->db->query($sql);
	
		$this->db->query("DELETE FROM " . DB_PREFIX . "return_status_description WHERE return_status_id = '" . (int)$return_status_id . "'");
		foreach ($data['return_status_description'] as $language_id => $value) {
			$sql_desc = "INSERT INTO " . DB_PREFIX . "return_status_description SET return_status_id = '" . (int)$return_status_id . 
				"', language_id = '" . (int)$language_id . 
				"', display_name = '";
			if($value['display_name']){
				$sql_desc .= $this->db->escape($value['display_name']);
			}else{
				$sql_desc .= $name;
			}
			$sql_desc .= "', description = '" . $this->db->escape($value['description']) . "'";
			$this->db->query($sql_desc);
		}
	}
	
	public function getReturnStatus($return_status_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "return_status WHERE return_status_id = '" . (int)$return_status_id . "'");
		return $query->row;
	}
	
	public function getReturnStatusDescriptions($return_status_id) {
		$return_status_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "return_status_description WHERE return_status_id = '" . (int)$return_status_id . "'");
		foreach ($query->rows as $result) {
			$return_status_data[$result['language_id']] = array(
				'display_name' => $result['display_name'],
				'description'  => $result['description']
			);
		}
		
		return $return_status_data;
	}
	
	public function getReturnStatuses() {
		$sql = "SELECT rs.*, rsd.display_name FROM " . 
			DB_PREFIX . "return_status rs LEFT JOIN " . 
			DB_PREFIX . "return_status_description rsd ON rs.return_status_id = rsd.return_status_id AND rsd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "' ORDER BY rs.`name`";
			
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
}
?>